package com.xxxx.traitTest

/**
 * @program: day0303
 * @description: 尽信书，则不如无书
 * @author: CoreDao
 * @create: 2021-03-03 16:36
 * */

/**
 * 坐标轴上的点比较
 */
trait Equal{
  def isEqual(x:Any):Boolean
  def isNotEqual(x:Any):Boolean={
    !isEqual(x)
  }
}

class Point(xx:Int,yy:Int) extends Equal{
  //横纵坐标
  val x = xx
  val y = yy

  //scala中判断一个对象是否是某个类的实例化对象
  //isInstanceOf
  //注意先判断，再转换，如果对象是Null,那么操作会返回 false null
  override def isEqual(p: Any): Boolean = {
    if (p.isInstanceOf[Point]){
      val point = p.asInstanceOf[Point]
      x == point.x && y == point.y
    }else{
      false
    }
  }
}

object Trait2 {
  def main(args: Array[String]): Unit = {
    val point = new Point(xx = 1, yy = 2)
    val point1 = new Point(xx = 2, yy = 3)
    println(point.isEqual(point1))

  }
}
